import Vue from 'vue'
import moment from 'moment'
// 时间
Vue.filter('moment', function(value, format) {
    return value ? moment(value - 0).format(format || 'YYYY-MM-DD H:mm:ss') : '';
});

// 高亮
Vue.filter('highlight', (value, keyword, isHeightLight = true) => {
    if (!isHeightLight) {
        return value;
    }
    return highlight(value, keyword);
});

// 话题高亮
Vue.filter('topicHighlight', (value, keyword) => {

    let keys = [];
    if (!Array.isArray(keyword)) {
        keys = [`#${keyword}#`];
    } else {
        keys = keyword.map(i => {
            return `#${i}#`
        });
    }

    return highlight(value, keys);
});

function highlight(value, keyword) {

    if(!value) return value;

    let keys = [];
    if (!Array.isArray(keyword)) {
        keys = [keyword];
    } else {
        keys = keyword;
    }

    // 处理关键字里面的特殊字符
    keys = [].concat(keys).map(i => {
        return i.replace(/\||\/|\\|\$|\(|\)|\*|\+|\.|\[|\]|\?|\^|\{|\}/gi, (k) => {
            return `\\${k}`
        })
    });

    let keysString = keys.join('|');

    if (!keysString) return value;

    let reg = eval(`/${keysString}/gi`);

    value = value.replace(reg, v => {
        return `<span style="color: #3462FF">${v}</span>`
    });
    return value
}

Vue.filter('formatTime', (value, isFromNow) => {
    if (!value) { return }
    const _fulldate = moment(value - 0).format('YYYY-MM-DD H:mm');
    const _date = moment(value - 0).format('MM-DD H:mm');
    const _time = moment(value - 0).format('H:mm');
    const date = new Date(value);
    const [date_year, date_month, date_days, date_hour, date_min, date_sec] = [
        date.getFullYear(),
        date.getMonth() + 1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds()
    ];
    const currentDate = new Date();
    const [current_year, current_month,current_days, current_hour, current_min, current_sec] = [
        currentDate.getFullYear(),
        currentDate.getMonth() + 1,
        currentDate.getDate(),
        currentDate.getHours(),
        currentDate.getMinutes(),
        currentDate.getSeconds()
    ];
    // console.log('实际时间', [date_year, date_month, date_days, date_hour, date_min, date_sec], '当前时间', [current_year, current_month, current_days, current_hour, current_min, current_sec])
    if (date_year < current_year) {
        return isFromNow ? _fulldate : `${current_year - date_year}年前`
    } else if (date_year === current_year && date_month < current_month) {
        return isFromNow ? _date : `${current_month - date_month}月前`
    } else if (date_year === current_year && date_month === current_month && date_days < current_days) {
        let day = current_days - date_days;
        return `${day === 1 ? '昨天 ' + _time : isFromNow ? _date : day + '天前'}`
    } else if (date_year === current_year && date_month === current_month && date_hour < current_hour) {
        return isFromNow ? '今天 ' + _time : `${current_hour - date_hour}小时前`
    } else if (date_year === current_year && date_month === current_month && date_hour === current_hour && date_min < current_min) {
        return `${current_min - date_min}分钟前`
    } else if (date_year === current_year && date_month === current_month && date_hour === current_hour && date_min === current_min && date_sec < current_sec) {
        return `${current_sec - date_sec}秒钟前`
    }
    return ''
});
